package com.cm.controller;

import com.cm.pojo.Result;
import com.cm.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * ClassName:  UploadController
 * Package: com.cm.controller
 * Description:
 *
 * @Author:
 * @Create: 2025/9/22  15:04
 * @Version 1.0
 */
@RestController
@Slf4j
public class UploadController {

    /*
        本地上传方案，不推荐
    * */
//    @PostMapping("/upload")
//    public Result upload(String name,Integer age,MultipartFile file) throws IOException {
//        log.info("接受参数{}，{}，{}",name,age,file);
//
//        //获取唯一上传的文件名
//        String uuid = java.util.UUID.randomUUID().toString();
//        String filename = uuid + "_" + file.getOriginalFilename();
//        //存储到本地
//        String originalFilename = file.getOriginalFilename();
//        file.transferTo(new File("D:/imag/" + filename));
//        return Result.success();
//    }


    /*
        阿里云上传方案
    * */

@Autowired
private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传文件{}",file.getOriginalFilename());
        String url=aliyunOSSOperator.upload( file.getBytes(),file.getOriginalFilename());
        return Result.success(url);

    }
}
